<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>点击列表项高亮显示</title>
    <style>
        .active {
            color: white;
            background-color: blue;
        }
    </style>
</head>
<body>
<div id="app">
    <ul>
        <li v-for="(item, index) in items" v-on:click="itemClicked(index)" v-bind:key="index"
            v-bind:class="{active: currentIndex === index}">{{ item }}
        </li>
    </ul>
</div>
<script src="../../../../../js/vue.global.js"></script>
<script>
    const app = Vue.createApp({
        data() {
            return {
                items: ["霸王别姬", "夜半歌声", "为你钟情", "无间道"],
                currentIndex: null,
            }
        },
        methods: {
            itemClicked(index) {
                console.log("点击了列表项:" + index);
                console.log("点击项的内容是:" + this.items[index]);
                this.currentIndex = index
            }
        }
    })

    app.mount("#app")
</script>
</body>
</html>